package top.glkj.hms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.glkj.hms.dao.UserDetailDao;
import top.glkj.hms.entity.UserDetail;
import top.glkj.hms.service.UserDetailService;
import top.glkj.hms.utils.Result;
import top.glkj.hms.utils.ResultFactory;
import top.glkj.hms.utils.StaticValue;

import java.util.List;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/16 23:45
 */
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailService {

    private UserDetailDao userDetailDao;

    @Autowired
    public UserDetailServiceImpl(UserDetailDao userDetailDao) {
        this.userDetailDao = userDetailDao;
    }

    /**
     * 获取所有用户，按分页方式
     *
     * @return Page<T>
     */
    @Override
    public Result getAll() {
        List<UserDetail> all = userDetailDao.findAll();
        if (all.size() == 0) {
            return ResultFactory.isError(StaticValue.DATA_NOT_FOUND, null);
        } else {
            return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS, all);
        }
    }

    /**
     * 获取所有用户，按分页方式
     *
     * @param id id
     * @return T t
     */
    @Override
    public Result getOne(Long id) {
        UserDetail detai = userDetailDao.findById(id).orElse(null);
        if (detai == null) {
            return ResultFactory.isError(StaticValue.DATA_NOT_FOUND, null);
        } else {
            return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS, detai);
        }
    }

    /**
     * 修改或者保存
     *
     * @param userDetail T t
     * @return true or false
     */
    @Override
    public Result save(UserDetail userDetail) {
        return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS, userDetailDao.save(userDetail));
    }

    /**
     * 彻底从数据库中删除实体{T}
     *
     * @param userDetail T t
     * @return true or false
     */
    @Override
    public Result delete(UserDetail userDetail) {
        userDetailDao.delete(userDetail);
        return ResultFactory.SUCCESS;
    }

    /**
     * 标记删除
     *
     * @param userDetail T t
     * @return true or false
     */
    @Override
    public Result remove(UserDetail userDetail) {
        userDetail.setDel(true);
        return ResultFactory.SUCCESS;
    }
}
